<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>4_只有watch才能实现</title>
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <div id="demo">
            姓: <input type="text" v-model="firstName"><br><br>
            名: <input type="text" v-model="lastName"><br><br>
            全名: <span>{{fullName}}</span>
        </div>
    </body>

    <script type="text/javascript">
        Vue.config.productionTip = false;

        new Vue({
            el: '#demo',
            data: {
                firstName: 'zhang',
                lastName: 'san',
                fullName: ''
            },
            watch: {
                firstName: {
                    immediate: true,
                    handler(val){
                        this.fullName = val + '-' + this.lastName
                    }
                },
                lastName(val){
                    setTimeout(() => {
                        this.fullName = this.firstName + '-' + val
                    }, 1000)
                }
            }
        })

    </script>
</html>